package com.basic.library.utils

import android.annotation.SuppressLint
import android.content.Context
import android.os.Build
import java.util.*


/**
 * Created by YYS on 2019/9/16.
 */
object DeviceUtil {

    /**
     * 获取设备唯一标识符
     *
     * @return 唯一标识符
     */
    @SuppressLint("HardwareIds")
    fun getDeviceId(context: Context): String {
        val m_szDevIDShort = ("35" + Build.BOARD.length % 10
                + Build.BRAND.length % 10 + Build.CPU_ABI.length % 10
                + Build.DEVICE.length % 10 + Build.DISPLAY.length % 10
                + Build.HOST.length % 10 + Build.ID.length % 10
                + Build.MANUFACTURER.length % 10 + Build.MODEL.length % 10
                + Build.PRODUCT.length % 10 + Build.TAGS.length % 10
                + Build.TYPE.length % 10 + Build.USER.length % 10)// 13 位

        var serial = "serial"// 默认serial可随便定义
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//                if (ActivityCompat.checkSelfPermission(context, READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
//                    // 由于 Android Q 唯一标识符权限的更改会导致
//                    // android.os.Build.getSerial() 返回 unknown,
//                    // 但是 m_szDevIDShort 是由硬件信息拼出来的，所以仍然保证了UUID 的唯一性和持久性。
//                    serial = Build.getSerial()// Android Q 中返回 unknown
//                }
            } else {
                serial = Build.SERIAL
            }
        } catch (ignored: Exception) {
        }

        return UUID(m_szDevIDShort.hashCode().toLong(), serial.hashCode().toLong()).toString()
    }
}